package com.hubox.manual;

import jakarta.validation.*;
import lombok.extern.slf4j.Slf4j;

import java.util.Set;

@Slf4j
public class ValidUtil {

    private static Validator createValidator(){
        Configuration<?> configure = Validation.byDefaultProvider().configure();
        ValidatorFactory factory = configure.buildValidatorFactory();
        Validator validator = factory.getValidator();
        factory.close();
        return validator;
    }

    public static <T> void valid(T obj){
        Validator validator = createValidator();
        Set<ConstraintViolation<T>> violationSet = validator.validate(obj);
        if(!violationSet.isEmpty()){
            String message = violationSet.stream().findFirst().get().getMessage();
            throw new IllegalArgumentException(message);
        }
    }
}
